package com.bluemoon.daps.test.sync.v1_3.devimport;

import cn.com.bluemoon.daps.common.enums.SyncToEnv;
import cn.com.bluemoon.daps.common.toolkit.BmAssetUtils;
import cn.com.bluemoon.daps.common.toolkit.SshUtils;
import cn.com.bluemoon.daps.sync.DapsSyncApplication;
import cn.com.bluemoon.daps.sync.base.CompareResult;
import cn.com.bluemoon.daps.sync.base.DevModelCompareResult;
import cn.com.bluemoon.daps.sync.config.PtServerConfig;
import cn.com.bluemoon.daps.sync.core.sync.GlobalConfig;
import cn.com.bluemoon.daps.sync.feignclient.DapMetadataModuleFacade;
import cn.com.bluemoon.daps.sync.service.DapDev2ModelSyncService;
import cn.com.bluemoon.daps.sync.service.DapSyncBasicService;
import cn.com.bluemoon.daps.sync.vo.dev2model.DatasourceOut;
import cn.com.bluemoon.daps.sync.vo.dev2model.MetaTableOut;
import cn.com.bluemoon.metadata.inter.dto.out.SchemaInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.List;

/**
 * @author Jarod.Kong
 * @date 2021/6/17 14:55
 */
@SpringBootTest(classes = DapsSyncApplication.class)
@RunWith(value = SpringRunner.class)
@Slf4j
public class Dev2ModelTestV3 {

    @Autowired
    private DapDev2ModelSyncService syncService;

    @Autowired
    private DapMetadataModuleFacade metadataModuleFacade;

    @Autowired
    private DapSyncBasicService syncBasicService;
    String modelId = "1420639495719301121";

    @Autowired
    private PtServerConfig ptServerConfig;

    @Test
    public void test_metadata() {
        GlobalConfig globalConfig = syncBasicService.buildSyncGlobalConfig(modelId, SyncToEnv.MODEL);
        SchemaInfoVO schemaInfoVO = metadataModuleFacade.getSchemaInfoVO(globalConfig.getDataSourceAndEnv(), globalConfig.getTableIdentity().getTableNamesDistinct());
        System.out.println(schemaInfoVO);
    }

    @Test
    public void test() {
        String s = SshUtils.client().sshInfo(ptServerConfig.getPtSsh()).runScriptAsset("ls -l");
        System.out.println(s);
        s = SshUtils.client().sshInfo(ptServerConfig.getPtSsh()).runScriptAsset("ls -l");
        System.out.println(s);
        s = SshUtils.client().sshInfo(ptServerConfig.getPtSsh()).runScriptAsset("ls -l");
        System.out.println(s);
        s = SshUtils.client().sshInfo(ptServerConfig.getPtSsh()).runScriptAsset("ls -l");
        System.out.println(s);
    }

    @Test
    public void test_1() {
        GlobalConfig.DataSourceAndEnv dsByModelId = syncService.getDsByModelId(modelId);
        BmAssetUtils.notNull(dsByModelId);
    }

    @Test
    public void test_2() {
        DatasourceOut datasourceOut = syncService.queryDsInfoByModelId(modelId);
        BmAssetUtils.notNull(datasourceOut);
    }

    @Test
    public void test_3() {
        List<MetaTableOut> metaTableOuts = syncService.queryDsTableInfoByModelId(modelId);
        BmAssetUtils.isFalse(metaTableOuts.isEmpty());
    }

    @Test
    public void test_4() {
        List<CompareResult<DevModelCompareResult>> compareResults = syncService.compareDevAndModelTables(modelId, Arrays.asList("dap_data_model_version"));
        BmAssetUtils.notNull(compareResults);
    }



    @Test
    public void test_api_1() {
        List<CompareResult<DevModelCompareResult>> compareResults1 = syncService.compareDevAndModelTables(modelId);
        List<CompareResult<DevModelCompareResult>> compareResults = syncService.compareDevAndModelTables2DB(modelId);
        log.info("{}", compareResults);
    }
}
